package com.yanqu.road.server.cmd.manor.fishpond;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionwar.UserFishSendTimes;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.manor.FishData;
import com.yanqu.road.pb.activity.RankUnionWarProto;
import com.yanqu.road.pb.manor.FishPondProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.manor.ManorModule;
import com.yanqu.road.server.manger.activity.rankunionwar.RankUnionWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWar;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2021/11/5 17:08
 */
@Cmd(code = Protocol.S_FISH_ACTIVITY_WAR_TIMES, desc = "鱼类的活动出战次数")
public class FishActivityWarTimesCmd extends AbstractCommand {
    private int activityId;
    private int activityType;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        FishPondProto.FishActivityWarTimesReqMsg reqMsg = FishPondProto.FishActivityWarTimesReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        activityType = reqMsg.getActivityType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (eActivityType.CrossUnionWarActivity.getValue() == activityType) {
            ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
            if (activityInfo == null) {
                return;
            }
        } else if(eActivityType.RankUnionWar.getValue() == activityType){
            if(RankUnionWarMgr.getActivityId() != activityId){
                return;
            }
        }else {
            return;
        }

        Map<Integer, FishData> userFishMap = player.getModule(ManorModule.class).getManorFishPondModel().getFishDataMap();
        List<FishData> userFishList = new ArrayList<>(userFishMap.values());
        if (eActivityType.CrossUnionWarActivity.getValue() == activityType) {
            Map<Integer, UserFishSendTimes> userFishSendTimesMap = null;
            UnionWar unionWar = UnionPhaseWarMgr.getUnionWar(activityId);
            if (unionWar != null) {
                if (unionWar.getUserFishSendMap().containsKey(player.getUserId())) {
                    userFishSendTimesMap = unionWar.getUserFishSendMap().get(player.getUserId());
                }
            }
            FishPondProto.FishActivityWarTimesRespMsg.Builder respMsg = FishPondProto.FishActivityWarTimesRespMsg.newBuilder();
            for (FishData fishData : userFishList) {
                FishPondProto.FishActivityWarTimesTempMsg.Builder temp = FishPondProto.FishActivityWarTimesTempMsg.newBuilder();
                temp.setFishId(fishData.getFishId());
                if (userFishSendTimesMap != null && userFishSendTimesMap.containsKey(fishData.getFishId())) {
                    UserFishSendTimes sendTimes = userFishSendTimesMap.get(fishData.getFishId());
                    temp.setWarTimes(sendTimes.getTimes());
                } else {
                    temp.setWarTimes(0);
                }
                respMsg.addTimes(temp);
            }
            respMsg.setRet(0);
            respMsg.setActivityId(activityId);
            respMsg.setActivityType(activityType);
            player.sendPacket(Protocol.U_FISH_ACTIVITY_WAR_TIMES, respMsg);
        }else if(eActivityType.RankUnionWar.getValue() == activityType){
            RankUnionWarProto.CrossFishActivityWarTimesReqMsg.Builder reqMsg = RankUnionWarProto.CrossFishActivityWarTimesReqMsg.newBuilder();
            for(FishData fishData : userFishList){
                FishPondProto.FishActivityWarTimesTempMsg.Builder temp = FishPondProto.FishActivityWarTimesTempMsg.newBuilder();
                temp.setFishId(fishData.getFishId());
                temp.setWarTimes(0);
                reqMsg.addTimes(temp);
            }
            reqMsg.setActivityId(activityId);
            reqMsg.setActivityType(activityType);
            player.sendPacket(Protocol.C2_CROSS_RANKUNIONWAR_FISH_TIMES, reqMsg);
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
